package com.htudata.common.service;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataEhcacheService {

  private static final String CACHENAME = "dataCache";
  
  @Autowired
  private CacheManager cacheManager;
  
  public Object getValueFromCache(Object cacheKey) {
    Cache forcastCache = this.cacheManager.getCache("dataCache");
    Element forcastElement = forcastCache.get(cacheKey);
    if (forcastElement != null)
      return forcastElement.getValue(); 
    return null;
  }
  
  public void putValueToCache(Object cacheKey, Object cacheValue) {
    Cache forcastCache = this.cacheManager.getCache("dataCache");
    Element element = new Element(cacheKey, cacheValue);
    forcastCache.put(element);
    forcastCache.flush();
  }
  
  public void deleteCache(Object cacheKey) {
    Cache forcastCache = this.cacheManager.getCache("dataCache");
    forcastCache.remove(cacheKey);
  }
}
